/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.manage.voucher;
import shop.core.common.bean.JSONResult;
import shop.core.common.dao.page.PagerModel;
import shop.core.util.KeyValueHelper;
import shop.core.Services;
import shop.core.util.MD5;
import shop.services.manage.voucher.VoucherService;
import shop.services.manage.voucher.bean.Voucher;
import shop.core.BaseController;
import shop.core.RequestHolder;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import shop.services.manage.vouchersend.VouchersendService;
import shop.services.manage.vouchersend.bean.Vouchersend;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**   
 * @类名称：VoucherAction      
 * @创建人：wzl
 * @创建时间：2018-01-03 下午16:55:48      
 * @版本号：1.0
 * @描述：     
 */
@Controller
@RequestMapping("/manage/voucher/")
public class VoucherAction extends BaseController<Voucher> {
    private static final Logger logger = LoggerFactory.getLogger(VoucherAction.class);
    @Autowired
    private VoucherService voucherService;
    @Autowired
    private VouchersendService vouchersendService;
    private static final String page_toList = "/manage/voucher/voucherList";
    private static final String page_toAdd = "/manage/voucher/voucherAdd";
    private static final String page_toEdit = "/manage/voucher/voucherEdit";
    private static final String page_toVoucherSend = "/manage/voucher/voucherSend";
    public VoucherService getService() {
        return voucherService;
    }

    private VoucherAction(){
        super.page_toList = page_toList;
        super.page_toAdd = page_toAdd;
        super.page_toEdit = page_toEdit;
    }

    /**
     * 跳转到礼卷派送记录页面
     *
     * @return
     */
    @RequestMapping("toVoucherSend")
    public String toVoucherSend() {
        return page_toVoucherSend;
    }

    /**
     * 查询礼卷列表
     *
     * @param request 请求
     * @param voucher       对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectVoucherPage")
    @ResponseBody
    public PagerModel selectVoucherPage(HttpServletRequest request, Voucher voucher) {
        int offset = 0;
        int pageSize = 10;
        int start=Integer.parseInt(request.getParameter("start"));
        int length= Integer.parseInt(request.getParameter("length"));
        if(start>0)
            offset=start;
        if(length>0)
            pageSize=length;
        voucher.setOffset(offset);
        voucher.setPageSize(pageSize);
        PagerModel pager = voucherService.selectVoucherPage(voucher);
        List<Voucher> voucherList = pager.getList();
        for(Voucher voucherInfo :voucherList){
            Vouchersend vouchersend=new Vouchersend();
            vouchersend.setVoucherID(voucherInfo.getId());
            List<Vouchersend> vouchersendList=vouchersendService.selectList(vouchersend);
            if(vouchersendList!=null&&vouchersendList.size()>0){
                voucherInfo.setSendAmount(vouchersendList.size());
            }else{voucherInfo.setSendAmount(0);}
        }
        pager.setList(voucherList);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    /**
     * 查询礼卷派送记录
     *
     * @return jsonResult
     */
    @RequestMapping("selectVoucherSendList")
    @ResponseBody
    public JSONResult selectVoucherSendList(Vouchersend vouchersend){
        List<Vouchersend> vouchersendList = vouchersendService.selectVoucherSendList(vouchersend);
        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(vouchersendList);
        return jsonResult;
    }
}
